package com.lyz.monitor.impl;

import org.apache.log4j.Logger;

import com.lyz.config.ConfigFactory;
import com.lyz.config.IConfig;
import com.lyz.file.IFileQueue;
import com.lyz.monitor.AbstractMonitorData;
import com.lyz.monitor.IMonitor;
import com.lyz.util.BaseTypeUtil;
import com.lyz.util.PropertiesUtil;
/**
 * 实时性监控的数据是文件队列中文件的数量
 * @author luzi
 *
 */
public class RealTimeMonitorData extends AbstractMonitorData {
	
	private static final Logger logger = Logger.getLogger(RealTimeMonitorData.class);
	
	private IConfig proUtil = ConfigFactory.getConfig();
		
	public RealTimeMonitorData(String tag, IFileQueue queue){
		this.tag = tag;
		this.obj = queue;
		this.condition = proUtil.getRealTimeCondition(tag);
		
	}

	@Override
	public int need2Alert() {
		
		try {
			if(!(obj instanceof IFileQueue)){
				logger.error("paramter is not a instance of IFileQueue");
				return -1;
			}
			
			int queueLength = BaseTypeUtil.getInteger(this.condition, 0);
			IFileQueue queue = (IFileQueue)this.obj;
			if(queue.getFileCount() >= queueLength){
				this.setAletMsg(this.tag+" file queue is "+queue.getFileCount());
				return IMonitor.LEVEL_MESSAGE;
			}else{
				this.setAletMsg("");
			}
		} catch (Exception e) {
			logger.error("fail to realTime alert!",e);
			this.setAletMsg(this.tag+" fail to realTime alert!");
			return IMonitor.LEVEL_MESSAGE;
		}
		
		return -1;	
		
	}

}
